﻿
function EditPostHandler() {
    this.$posterTextbox = $("#newposter");
    this.$posterList = $("#PosterList");
    this.$linkTextbox = $("#newlink");
    this.$linkList = $("#LinkList");
    this.$issueTextbox = $("#newissue");
    this.$issueList = $("#IssueList");
    this.$saveButton = $("#saveButton");
    this.service = new BlogStudyService();

    window.PageBus.subscribe(EditPostHandler.refresh, this, this.refreshHandler);
    window.PageBus.subscribe(EditPostHandler.save, this, this.saveHandler);
    window.PageBus.subscribe(EditPostHandler.saveCallback, this, this.saveCallback, this.$saveButton);
    window.PageBus.subscribe(EditPostHandler.remove, this, this.removeHandler);


    window.PageBus.subscribe(EditPostHandler.addPoster, this, this.addHandler,
        new Add(this.$posterTextbox, this.service.addPoster, EditPostHandler.addPosterCallback)
    );
    window.PageBus.subscribe(EditPostHandler.addPosterCallback, this, this.addCallback, this.$posterList);

    window.PageBus.subscribe(EditPostHandler.addIssue, this, this.addHandler,
        new Add(this.$issueTextbox, this.service.addIssue, EditPostHandler.addIssueCallback)
    );
    window.PageBus.subscribe(EditPostHandler.addIssueCallback, this, this.addCallback, this.$issueList);

    window.PageBus.subscribe(EditPostHandler.addLink, this, this.addHandler,
        new Add(this.$linkTextbox, this.service.addLink, EditPostHandler.addLinkCallback)
    );
    window.PageBus.subscribe(EditPostHandler.addLinkCallback, this, this.addCallback, this.$linkList);

    window.PageBus.subscribe(EditPostHandler.remove, this, this.dirtyHandler, this.$saveButton);
    window.PageBus.subscribe(EditPostHandler.add, this, this.dirtyHandler, this.$saveButton);
}
EditPostHandler.prototype.$posterList = null;
EditPostHandler.prototype.$linkList = null;
EditPostHandler.prototype.$issueList = null;
EditPostHandler.prototype.isDirty = false;
EditPostHandler.prototype.$posterTextbox = null;
EditPostHandler.prototype.service = null;
EditPostHandler.refresh = 'blog.post.refresh';
EditPostHandler.save = 'blog.post.save';
EditPostHandler.saveCallback = 'blog.post.save.callback';
EditPostHandler.remove = 'blog.post.*.remove';
EditPostHandler.add = 'blog.post.*.add';
EditPostHandler.removePoster = 'blog.post.poster.remove';
EditPostHandler.removeLink = 'blog.post.link.remove';
EditPostHandler.removeIssue = 'blog.post.issue.remove';
EditPostHandler.addPoster = 'blog.post.poster.add';
EditPostHandler.addPosterCallback = 'blog.post.poster.add.callback';
EditPostHandler.addLink = 'blog.post.link.add';
EditPostHandler.addLinkCallback = 'blog.post.link.add.callback';
EditPostHandler.addIssue = 'blog.post.issue.add';
EditPostHandler.addIssueCallback = 'blog.post.issue.add.callback';

EditPostHandler.prototype.refreshHandler = function (subj, msg, data) {
    $("#refreshForm").submit();
};
EditPostHandler.prototype.dirtyHandler = function (subj, msg, data) {
    //change save icon
    this.isDirty = true;
    data.addClass("ui-state-highlight");
};
EditPostHandler.prototype.saveHandler = function (subj, msg, data) {
    //alert("save Post");
    var post = {};
    post.ID = msg;
    post.BlogName = $("#Post_BlogName").val();
    post.Permalink = $("#Post_Permalink").val();
    post.Published = $('#Post_Pubdate').val();
    post.Title = $("#Post_Title").val();
    post.Posters = [];
    var posters = this.$posterList.find("span.poster");
    for (var i = 0; i < posters.length; i++) {
        var attr = $(posters[i]).attr("id");
        var id = attr.split('-')[1];
        post.Posters.push(id);
    }
    post.Links = [];
    this.$linkList.children().each(function (liIndex) {
        var tone = $(this).find("label[aria-pressed='true'] span").text();
        var span = $(this).find("span.link").first();
        var link = [];
        var id = $(span).attr("id");
        link[0] = id.split('-')[1];
        link[1] = tone;
        post.Links.push(link);
    });
    post.Issues = [];
    this.$issueList.children().each(function (liIndex) {
        var affectiveFrame = $(this).find("label[aria-pressed='true'] span").text();
        var span = $(this).find("span.issue").first();
        var issue = [];
        var id = $(span).attr("id");
        issue[0] = id.split("-")[1];
        issue[1] = affectiveFrame;
        post.Issues.push(issue);
    });
    function callback(result, message) {
        window.PageBus.publish(EditPostHandler.saveCallback, result);
    }
    this.service.savePost(post, callback);
};
EditPostHandler.prototype.saveCallback = function (subj, msg, data) {
    alert(msg.Message);
    data.removeClass("ui-state-highlight");
    this.isDirty = false;
};
EditPostHandler.prototype.removeHandler = function (subj, msg, data) {
    $(msg).parent().remove();
};
EditPostHandler.prototype.addHandler = function (subj, postID, data) {
    var dataToAdd = data.element.val();
    if (dataToAdd && dataToAdd.length > 0) {
        var subject = data.callback;
        data.method(postID, dataToAdd, function (result) {
            window.PageBus.publish(subject, result);
        });
    } else {
        alert("fail");
    }
};
EditPostHandler.prototype.addCallback = function (subj, msg, data) {
    alert(msg.Message);
    if (msg.Html.length > 0) {
        data.append("<li>" + msg.Html + "</li>");
        $(".delete").button({ icons: { primary: 'ui-icon-trash' }, text: false });
        $(".radiosAsButtons").buttonset();
    }
};

function BlogStudyService() { }
BlogStudyService.prototype.addPoster = function (postID, poster, callback) {
    var arg = { PostID: postID, AliasName: poster };
    Service.post("/Blog/AddPoster", arg, callback, "json");
};
BlogStudyService.prototype.addIssue = function (postID, topic, callback) {
    var arg = { PostID: postID, TopicName: topic };
    Service.post("/Blog/AddIssue", arg, callback, "json");
};
BlogStudyService.prototype.addLink = function (postID, link, callback) {
    var arg = { PostID: postID, Href: link };
    Service.post("/Blog/AddLink", arg, callback, "json");
};
BlogStudyService.prototype.getBlogAliases = function (blogName, callback) {
    Service.get("/Blog/GetBlogAliases", blogName, callback, "json");
};
BlogStudyService.prototype.getProximateTopics = function (postID, callback) {
    Service.get("/Blog/GetProximateTopics", blogName, callback, "json");
};
BlogStudyService.prototype.savePerson = function () { };
BlogStudyService.prototype.savePost = function (_post, callback) {
    var _data = JSON.stringify(_post);
    $.ajax({ type: 'POST', url: "/Blog/SavePost", data: _data, success: callback, dataType: "json", contentType: "application/json; charset=utf-8", traditional: true });
};
BlogStudyService.prototype.mergeTopics = function () { };
